<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <link rel="shortcut icon" href="./images/favicon.ico" type="image/x-icon"> -->
    <!-- /favicon.ico -->
    <!-- <script src="./js/JavaScript.js"></script>
    <link rel="stylesheet" href="./css/reset.css" type="text/css">
    <link rel="stylesheet" href="./css/style.css" type="text/css"> -->
    <script src="../js/jquery/jquery-3.2.1.js"></script>
    <style>
        .inputs {
            margin: 100px auto;
            width: 850px;
        }

        .inputs input {
            width: 200px;
            height: 30px;
            font-size: 18px;
        }

        table {
            margin: 100px auto;
            width: 850px;
        }

        table tr td,
        table tr th {
            width: 200px;
            text-align: center;
            border: 1px solid black;
            height: 30px;
        }

        button {
            margin: 100px auto;
            display: block;
        }
    </style>
</head>

<body>
    <div class="inputs">
        <input type="text" id="name" placeholder="姓名">
        <input type="text" id="year" placeholder="年龄">
        <input type="text" id="date" placeholder="出生日期">
        <input type="text" id="from" placeholder="籍贯">
    </div>
    <button class="btn">提交</button>
    <table>

    </table>
</body>
<script>
    // localStorage.clear()
    var $but = $(".btn");
    console.log($but);
    var str = !(window.localStorage.getItem("data_arr")) ? [] : JSON.parse(window.localStorage.getItem("data_arr"));
    console.log(str)
    $but.click(function () {
        var data_1 = {
            name: $(".inputs input")[0].value,
            year: $(".inputs input")[1].value,
            date: $(".inputs input")[2].value,
            from: $(".inputs input")[3].value,
        }
        console.log(data_1)
        str.push(data_1);
        // console.log(str)
        window.localStorage.setItem("data_arr", JSON.stringify(str))
        // console.log(JSON.parse(window.localStorage.getItem("data_arr")));
        xuan();
    })

    function xuan() {
        var str_xuanran = `<tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>出生日期</th>
            <th>籍贯</th>
        </tr>`;
        for (var key of JSON.parse(window.localStorage.getItem("data_arr"))) {
            console.log(this)
            str_xuanran += `<tr>
            <td>${key.name}</td>
            <td>${key.year}</td>
            <td>${key.date}</td>
            <td>${key.from}</td>
        </tr>`
        }
        $("table").html(str_xuanran);
        // document.querySelector("table").innerHTML(str_xuanran);
        // console.log(document.querySelector("table"))
        // document.querySelector("table").innerHTML = str_xuanran;
    }
    xuan();
</script>

</html>